package com.home.truc.code.components;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;

import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JWindow;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JButton;

import com.home.Common.Common;
import com.home.Entity.Ban;
import com.home.Entity.Mon;
import com.home.Entity.TTGoiMon;
import com.home.duy.code.DAO.BanDAO;
import com.home.duy.code.DAO.MonDAO;
import com.home.duy.code.DAO.TTGoiMonDAO;
import com.home.truc.code.process.MyTimerHideComponent;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.SystemColor;
import javax.swing.border.LineBorder;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.border.TitledBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.UIManager;

public class ThongTinMonComponent extends JWindow {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	/**
	 * @param owner
	 */
	Frame parent = null;
	JList lMon = null;
	JPanel lGoiMon = null;
	JTabbedPane jpanelBan = null;
	public ThongTinMonComponent(Frame owner,JList LMon, JPanel LGoiMon, JTabbedPane panelBan) {
		super(owner);
		initialize();
		this.parent = owner;
		this.lMon = LMon;
		this.lGoiMon = LGoiMon;
		this.jpanelBan = panelBan;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(295, 170);
		this.setContentPane(getJContentPane());
		
		this.autoHide = new MyTimerHideComponent(2, this);
	}
	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	JButton btnGiMn;
	JButton btnHtHng ;
	JButton btnDong;
	JLabel lblTnMn;
	JLabel lblMT;
	JLabel lblGiBn;
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseEntered(MouseEvent arg0) {
					SetSelect_AutoHide(false);
				}
				@Override
				public void mouseExited(MouseEvent e) {
					SetSelect_AutoHide(true);
				}
			});
			jContentPane.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			jContentPane.setBackground(SystemColor.window);
			jContentPane.setLayout(new BorderLayout());
			
			JLabel lblThngTinMn = new JLabel("Thông tin món");
			lblThngTinMn.setFont(new Font("Tahoma", Font.BOLD, 12));
			lblThngTinMn.setHorizontalAlignment(SwingConstants.CENTER);
			jContentPane.add(lblThngTinMn, BorderLayout.NORTH);
			
			JPanel panel = new JPanel();
			panel.setBorder(null);
			panel.setBackground(SystemColor.window);
			jContentPane.add(panel, BorderLayout.SOUTH);
			
			btnGiMn = new JButton("Gọi món");
			btnGiMn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					GoiMon();
				}
			});
			panel.add(btnGiMn);
			
			btnHtHng = new JButton("Ngừng bán");
			btnHtHng.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {
						value.setTinhTrang(!value.isTinhTrang());
						MonDAO dao = new MonDAO();
						dao.saveOrUpdate(value);
						CapNhatGiaoDien();
					} catch (Exception e) {
						JOptionPane.showMessageDialog(jContentPane, "Không thể cập nhật được!!!");
					}
				}
			});
			panel.add(btnHtHng);
			
			btnDong = new JButton("Đóng");
			btnDong.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					hide();
				}
			});
			panel.add(btnDong);
			
			JPanel panel_1 = new JPanel();
			panel_1.setBorder(null);
			panel_1.setBackground(SystemColor.window);
			jContentPane.add(panel_1, BorderLayout.CENTER);
			panel_1.setLayout(null);
			
			lblTnMn = new JLabel("Tên món");
			lblTnMn.setBounds(10, 16, 230, 14);
			lblTnMn.setHorizontalAlignment(SwingConstants.LEFT);
			panel_1.add(lblTnMn);
			
			lblMT = new JLabel("Mô tả");
			lblMT.setBounds(10, 55, 230, 37);
			lblMT.setHorizontalAlignment(SwingConstants.LEFT);
			panel_1.add(lblMT);
			
			lblGiBn = new JLabel("Giá bán");
			lblGiBn.setBounds(10, 41, 230, 14);
			panel_1.add(lblGiBn);
		}
		return jContentPane;
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
	MyTimerHideComponent autoHide = null;
	
	private Mon value = null;
	
	
	public void SetSelect_AutoHide(boolean b) {
		this.autoHide.SetCanHide(b);
	}

	//cac phuong thuc xu ly
	public void SetInfo(Mon m )
	{
		try {
			this.value = m;	
			CapNhatGiaoDien();
		} catch (Exception e) {
		}	

	}
	public void CapNhatGiaoDien()
	{
		try {
			if(this.value!= null){
				if(this.value.getTen()!=null)
					this.lblTnMn.setText("Tên: "+this.value.getTen());
				else
					this.lblTnMn.setText("");
				
				this.lblGiBn.setText("Giá bán: "+this.value.getGiaBan());
				
				if(this.value.getMoTa()!=null)
					this.lblMT.setText("Mô tả: "+this.value.getMoTa());
				else
					this.lblMT.setText("");
				
				//kiem tra tinh trang mon
				if(this.value.isTinhTrang())
				{
					this.btnHtHng.setText("Ngừng bán");
					this.btnGiMn.setEnabled(true);
				}else
				{
					this.btnHtHng.setText("Bán lại");
					this.btnGiMn.setEnabled(false);
				}
			}
		} catch (Exception e) {
		}
	}

	//phuong thuc goi mon
	public boolean GoiMon()
	{
		try {
			MyJPanelKhuVucBan kv =  (MyJPanelKhuVucBan)this.jpanelBan.getSelectedComponent();
			
			Mon mon = (Mon)this.lMon.getSelectedValue();
			Ban ban = kv.getSelectItem().getValue();
			if(mon == null || kv == null)
				return false;
			
			if(!ban.isSuDung())
			{
				JOptionPane.showMessageDialog(this.parent, "Bàn này không được sử dụng!!!");
				return false;
			}
			//kiem tra tinh trang mon
			if(!mon.isTinhTrang())
			{
				JOptionPane.showMessageDialog(this.parent, "Món này không được bán!!!");
				return false;
			}
			
			//kiem tra nguyen lieu pha che***********************************************
			
			//tạo thông tin gọi món
			TTGoiMon m = new TTGoiMon();
			m.setBan(ban);
			m.setMaSo(Common.getID());
			m.setTgGoi(Calendar.getInstance().getTime());
			m.setMon((Mon)this.lMon.getSelectedValue());
			m.setDapUng(false);
			m.setTinhTien(false);
			
			new TTGoiMonDAO().saveOrUpdate(m);
			this.lGoiMon.add(new MyItemThongTinGoiMon(m, this.lGoiMon, this.jpanelBan));
			this.lGoiMon.updateUI();
			//cap nhat nguyen lieu*******************************************************
			
			//cap nhat tinh trang ban
			ban.setTrangThai(true);
			new BanDAO().saveOrUpdate(ban);
			kv.getSelectItem().CapNhatTinhTrang();
			
			return true;
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this.parent, "Bạn phải chọn bàn cần gọi trước!!!");
		}
		return false;
	}
}
